/**
 * Created by wdj on 15/11/24.
 */
;(function (hybrid) {

    hybrid.geolocation = {

        listens: {},

        /**
         * 获取当前位置
         * @param {Function} [callback]
         */
        getCurrentLocation: function(callback){

            hybrid.callAction("LocationManager/getCurrentLocation",{},function(result){
                typeof callback === 'function' && callback(result.status, result.data);
            });
        },

        /**
         * 监听位置
         * @param {*} options
         * @param {Function} callback
         * @return {String} 监听函数id
         */
        watchPosition: function(options, callback){
            var self = this;
            var id = new Date().getTime().toString(32);
            if(typeof options === 'function'){
                if(!callback){
                    callback = options;
                    options = {};
                }else{
                    options = options.call(self);
                }
            }
            self.listens[id] = function (e) {
                typeof callback === 'function' && callback(e.data);
            };
            options = hybrid.utils.extend({
                distanceFilter: 1.0 //定位频率,每隔多少米定位一次
            }, options);
            hybrid.callAction("LocationManager/watchPosition", options, function (result) {
                if (result.status) {
                    hybrid.addEventListener('watchPositionChanged', self.listens[id]);
                }
            });
            return id;
        },

        /**
         * 停止
         * @param {Number} id watchPosition返回的ID
         * @param {Function} callback
         */
        stopWatch: function(id, callback){
            var self = this;
            if(self.listens[id]){
                hybrid.removeEventListener('watchPositionChanged', self.listens[id]);
                delete self.listens[id];
            }
            hybrid.callAction("LocationManager/stopWatch",{},function(result){
                typeof callback === 'function' && callback(result.status, result.data);
            });
        }
    };

})(Hybrid || {});